package com.gzdh.tang.dishrecognition.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.tang.comm.vo.Result;
import com.gzdh.tang.modu.entity.SysUser;
import com.gzdh.tang.modu.entity.TangDevice;
import com.gzdh.tang.modu.mapper.OperaMapper;
import com.gzdh.tang.modu.service.MacMainpSbruanService;
import com.gzdh.tang.modu.service.TangDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("dr")
public class LoginController {

    @Autowired
    OperaMapper operaMapper;

    @Autowired
    TangDeviceService tangDeviceService;

    @Autowired
    MacMainpSbruanService macMainpSbruanService;


    @PostMapping("login")
    public Result<Object> login(@RequestParam("user") String user,
                                @RequestParam("pass") String pass,
                                @RequestParam("deviceId") String deviceId) throws Exception {
        Result<Object> result = new Result<>();
        SysUser sysUser = operaMapper.getSysUser(user);

        if (sysUser == null) {
            return Result.error(500, "用户编号或密码错误");
        }
        String password = sysUser.getPasswd().trim();
        byte[] bytes = password.getBytes("GBK");
        byte temp;
        for (int i = 0; i < bytes.length; i++) {
            temp = bytes[i];
            bytes[i] = (byte) ~temp;
        }
        String newPassword = new String(bytes, "GBK");
        if (!newPassword.equals(pass)) {
            return Result.error(500, "用户编号或密码错误");
        }

//        TangDevice tangDevice = tangDeviceService.getOne(new QueryWrapper<TangDevice>().lambda().eq(TangDevice::getPosnum, user));
//        if (tangDevice == null) {
//            return Result.error(500, "该操作员尚未设置操作信息");
//        }
        String resultString = sysUser.getXm() + "-" + sysUser.getGroup1() + "-" + sysUser.getAddr();
        result.setResult(resultString);
        macMainpSbruanService.getDeviceSign(deviceId);
        return result;
    }

    @PostMapping("deviceSign")
    public Result<Object> deviceSign(@RequestParam("deviceId")String deviceId) {
        Result<Object> result = new Result<>();
        String posInfo = macMainpSbruanService.getDeviceSign(deviceId);
        result.setResult(posInfo);
        return result;
    }

    @GetMapping("getServerTime")
    public Result<Object> getServerTime() {
        return new Result<>("查询成功", 0, System.currentTimeMillis());
    }
}
